package com.ruoyi.resume.service.impl;

import com.ruoyi.resume.domain.model.Category;
import com.ruoyi.resume.mapper.CategoryMapper;
import com.ruoyi.resume.mapper.JobMapper;
import com.ruoyi.resume.mapper.TagMapper;
import com.ruoyi.resume.service.CategoryService;
import com.ruoyi.resume.service.TagService;
import com.ruoyi.resume.task.TaskQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private TagService tagService;

    @Override
    public Integer createCategory(Category category) {
        // 获取当前数据库中的最大 ID
        Integer maxId = categoryMapper.getMaxCategoryId();
        category.setId(maxId + 1);

        // 插入新分类
        categoryMapper.insertCategory(category);

        // 判断并更新父级分类的 is_last_level 字段
        if (category.getParentId() != null) {
            Category parentCategory = categoryMapper.selectCategoryById(category.getParentId());
            if (parentCategory != null && Boolean.TRUE.equals(parentCategory.getIsLastLevel())) {
                // 父级分类有子分类后，将 is_last_level 修改为 false
                categoryMapper.updateIsLastLevel(parentCategory.getId(), false);
            }
        }
        return category.getId();
    }

    @Override
    public boolean deleteCategoryById(Integer id) {
        // 获取待删除分类的信息
        Category categoryToDelete = categoryMapper.selectCategoryById(id);
        if (categoryToDelete == null) {
            return false;
        }

        // 递归删除分类及其子分类
        deleteCategoryWithChildren(id);

        // 检查父级分类是否需要更新 is_last_level 字段
        if (categoryToDelete.getParentId() != null) {
            List<Category> siblings = categoryMapper.selectCategoriesByParentId(categoryToDelete.getParentId());
            if (siblings.isEmpty()) {
                // 如果父级分类已无子分类，将其 is_last_level 修改为 true
                categoryMapper.updateIsLastLevel(categoryToDelete.getParentId(), true);
            }
        }
        return true;
    }

    @Override
    public boolean updateCategory(Category category) {
        // 检查分类是否存在
        Category existingCategory = categoryMapper.selectCategoryById(category.getId());
        if (existingCategory != null) {
            // 更新分类信息
            categoryMapper.updateCategory(category);
            return true;
        }
        return false;
    }

    private void deleteCategoryWithChildren(Integer categoryId) {
        // 删除该分类下的所有子分类
        List<Category> childCategories = categoryMapper.selectCategoriesByParentId(categoryId);
        for (Category child : childCategories) {
            deleteCategoryWithChildren(child.getId());
        }

        // 删除与该分类相关的 tags
        tagService.deleteTagById(categoryId);

        // 删除当前分类
        categoryMapper.deleteCategoryById(categoryId);
    }
}
